package com.ruoyi.project.drugs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.PageDomain;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.page.TableSupport;
import com.ruoyi.project.system.domain.ShopCard;
import com.ruoyi.project.system.domain.ShopCardCount;
import com.ruoyi.project.system.domain.TDrugs;
import com.ruoyi.project.system.service.ITDrugsService;
import com.ruoyi.project.system.service.ShopCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/shop/card")
public class ShopCardController extends BaseController {

    @Autowired
    private ShopCardService shopCardService;
    @Autowired
    private ITDrugsService itDrugsService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public ShopCardCount list(ShopCard shopCard) {
        System.out.println("查询购物车请求参数"+shopCard);
        SecurityUtils.getLoginUser();
        if (SecurityUtils.getLoginUser().getUser().getRoles().get(0).getDataScope().equals("5")) {
            shopCard.setIsShelves("1");
            shopCard.setIsSelf("1");
        }
        startPage();
        shopCard.setFlag("0");
        shopCard.setCreateUser(SecurityUtils.getLoginUser().getUser().getUserName());
        List<ShopCard> list = shopCardService.selectShopCardList(shopCard);
        BigDecimal money =  BigDecimal.ZERO;
        List<ShopCard> shopCardList = new ArrayList<>();
        for (ShopCard td : list) {
            TDrugs tDrugs = itDrugsService.selectTDrugsById(td.getDrugId());
            td.setDataScope(SecurityUtils.getLoginUser().getUser().getRoles().get(0).getDataScope());
               money.add(td.getPrice().multiply(BigDecimal.valueOf(td.getNumber())));
               td.setPimage(tDrugs.getPimage());
            shopCardList.add(td);
         }
        ShopCardCount shopCardCount = new ShopCardCount();
        shopCardCount.setShopCard(shopCardList);
        shopCardCount.setCountMoney(money);
        return shopCardCount;
    }

    /**
     * 新增购物车
     *
     * @param shopCard 【请填写功能名称】
     * @return 结果
     */
    @PostMapping("/addShopCard")
    public AjaxResult insertShopCard(@RequestBody ShopCard shopCard){
        shopCard.setCreateUser(SecurityUtils.getLoginUser().getUser().getUserName());
        shopCard.setCreateDate(new Date());
        shopCard.setUpdateUser(SecurityUtils.getLoginUser().getUser().getUserName());
        shopCard.setUpdateDate(new Date());
        List<ShopCard> shopCardList = shopCardService.queryShopCardByUserAnddrugId(shopCard);
        if(shopCardList.size()>0){
            ShopCard shopCard1 = shopCardList.get(0);
            Integer num = shopCard1.getNumber();
            int countNumer =num+shopCard.getNumber();
            shopCard1.setNumber(Integer.valueOf(countNumer));
            shopCard1.setUpdateDate(new Date());
           return toAjax(shopCardService.updateShopCardNumberById(shopCard1));
        }else {
            return   toAjax(shopCardService.insertShopCard(shopCard));
        }

    }

    /**
     * 删除购物车商品
     * @param ids
     * @return
     */
    @PostMapping("/deleteShopCard")
    public AjaxResult deleteTDrugsByIds(@RequestBody String [] list){
        return toAjax(shopCardService.deleteTDrugsByIds(list));
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
}
